// 交互相关工具函数

import { TargetType, CommentOrderBy } from '../types';

// 评论内容最大长度
export const MAX_COMMENT_LENGTH = 500;

// 评论分页默认值
export const DEFAULT_COMMENT_PAGE = 1;
export const DEFAULT_COMMENT_SIZE = 20;
export const MAX_COMMENT_SIZE = 100;

// 验证评论内容
export const validateCommentText = (commentText: string): { valid: boolean; error?: string } => {
  if (!commentText || commentText.trim().length === 0) {
    return {
      valid: false,
      error: '评论内容不能为空'
    };
  }
  
  if (commentText.length > MAX_COMMENT_LENGTH) {
    return {
      valid: false,
      error: `评论内容不能超过${MAX_COMMENT_LENGTH}字符`
    };
  }
  
  return { valid: true };
};

// 验证目标类型
export const validateTargetType = (targetType: string): { valid: boolean; error?: string } => {
  if (!targetType) {
    return {
      valid: false,
      error: '目标类型不能为空'
    };
  }
  
  if (!Object.values(TargetType).includes(targetType as TargetType)) {
    return {
      valid: false,
      error: '目标类型只能是CONTENT或COMMENT'
    };
  }
  
  return { valid: true };
};

// 验证目标ID
export const validateTargetId = (targetId: number): { valid: boolean; error?: string } => {
  if (!targetId || targetId <= 0) {
    return {
      valid: false,
      error: '目标ID不能为空且必须大于0'
    };
  }
  
  return { valid: true };
};

// 验证内容ID
export const validateContentId = (contentId: number): { valid: boolean; error?: string } => {
  if (!contentId || contentId <= 0) {
    return {
      valid: false,
      error: '内容ID不能为空且必须大于0'
    };
  }
  
  return { valid: true };
};

// 验证评论ID
export const validateCommentId = (commentId: number): { valid: boolean; error?: string } => {
  if (!commentId || commentId <= 0) {
    return {
      valid: false,
      error: '评论ID不能为空且必须大于0'
    };
  }
  
  return { valid: true };
};

// 验证分页参数
export const validatePaginationParams = (
  page?: number, 
  size?: number
): { valid: boolean; error?: string } => {
  if (page && page <= 0) {
    return {
      valid: false,
      error: '页码必须大于0'
    };
  }
  
  if (size && (size <= 0 || size > MAX_COMMENT_SIZE)) {
    return {
      valid: false,
      error: `每页数量必须在1-${MAX_COMMENT_SIZE}之间`
    };
  }
  
  return { valid: true };
};

// 获取目标类型显示文本
export const getTargetTypeText = (targetType: TargetType): string => {
  switch (targetType) {
    case TargetType.CONTENT:
      return '内容';
    case TargetType.COMMENT:
      return '评论';
    default:
      return '未知';
  }
};

// 获取排序方式显示文本
export const getCommentOrderByText = (orderBy: CommentOrderBy): string => {
  switch (orderBy) {
    case CommentOrderBy.CREATED_AT:
      return '按时间排序';
    case CommentOrderBy.LIKE_COUNT:
      return '按热度排序';
    default:
      return '默认排序';
  }
};

// 解析点赞切换操作结果
export const parseLikeToggleResult = (result: string): { isLiked: boolean; message: string } => {
  const isLiked = result.includes('点赞成功');
  return {
    isLiked,
    message: result
  };
};

// 格式化评论时间
export const formatCommentTime = (createdAt: string): string => {
  const now = new Date();
  const commentTime = new Date(createdAt);
  const diff = now.getTime() - commentTime.getTime();
  
  const minutes = Math.floor(diff / (1000 * 60));
  const hours = Math.floor(diff / (1000 * 60 * 60));
  const days = Math.floor(diff / (1000 * 60 * 60 * 24));
  
  if (minutes < 1) {
    return '刚刚';
  } else if (minutes < 60) {
    return `${minutes}分钟前`;
  } else if (hours < 24) {
    return `${hours}小时前`;
  } else if (days < 7) {
    return `${days}天前`;
  } else {
    return commentTime.toLocaleDateString('zh-CN');
  }
};

// 截取评论内容（用于预览）
export const truncateCommentText = (text: string, maxLength: number = 100): string => {
  if (text.length <= maxLength) {
    return text;
  }
  
  return text.substring(0, maxLength) + '...';
};

// 检查是否可以删除评论（基于用户权限）
export const canDeleteComment = (
  commentUserId: number, 
  currentUserId: number, 
  userRole: string
): boolean => {
  // 管理员可以删除任意评论
  if (userRole === 'ADMIN' || userRole === 'SUPERADMIN') {
    return true;
  }
  
  // 用户只能删除自己的评论
  return commentUserId === currentUserId;
};

// 生成点赞按钮的样式类名
export const getLikeButtonClass = (isLiked: boolean, baseClass: string = 'like-btn'): string => {
  return isLiked ? `${baseClass} ${baseClass}--active` : baseClass;
};

// 格式化点赞数显示
export const formatLikeCount = (count: number): string => {
  if (count === 0) {
    return '点赞';
  } else if (count < 1000) {
    return count.toString();
  } else if (count < 10000) {
    return (count / 1000).toFixed(1) + 'k';
  } else {
    return (count / 10000).toFixed(1) + 'w';
  }
}; 